package com.bum.rss.site;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.InitializingBean;

import com.bum.rss.exp.om.RssEntry;
import com.bum.rss.site.cat.Kenh14Cat;
import com.bum.rss.site.util.BaseParser;

public class Kenh14Site extends Site implements Kenh14Cat, InitializingBean {
	final HashMap<String, String>	CAT_CODES	= new HashMap<String, String>();
	final List<RssEntry>			CATEGORIES	= new ArrayList<RssEntry>(CATS.length);

	@Override
	public void afterPropertiesSet() throws Exception {
		for (int n = CATS.length, i = 0; i < n; i++) {
			CAT_CODES.put(CATS[i][0], CATS[i][2]);
			CATEGORIES.add(new RssEntry(getCode() + URL_SEPARATOR + CATS[i][0], CATS[i][1]));
		}
	}

	@Override
	public List<RssEntry> listSiteCats(String user) {
		return CATEGORIES;
	}

	@Override
	public List<RssEntry> listCatEntrys(String cat, String user) {
		int page = 1;
		if (cat.indexOf(PAGE_SEPARATOR) > 0) {
			String[] parts = StringUtils.split(cat, PAGE_SEPARATOR);
			page = NumberUtils.toInt(parts[1], 1);
			cat = parts[0];
		}

		String url = "http://m.kenh14.vn/Ajax/AjaxList.aspx?page=" + page + "&catid=" + CAT_CODES.get(cat);
		String html = BaseParser.loadHtml(url, BaseParser.ANDROID_HEADERS, true);

		List<RssEntry> entries = new LinkedList<RssEntry>();

		String[] parts = html.split("<li>");
		for (int i = 1; i < parts.length - 1; i++) {
			String part = parts[i];
			String link = BaseParser.parseContent(part, "<a href=\"", "\" class");
			if (link == null) {
				continue;
			}
			if (!link.startsWith("http://")) {
				link = "http://m.kenh14.vn" + link;
			}
			
			link = getRssLink(link, cat);
			if (link == null) {
				continue;
			}

			String img = BaseParser.parseContent(part, "<img class=\"catehot-thumbnail\" src=\"", "\" alt=");
			String title = BaseParser.parseContent(part, "<h2 class=\"catehot-title\">", "</h2>");
			String intro = BaseParser.parseContent(part, "<p class=\"catehot-sapo\">", "</p>");

			RssEntry entry = new RssEntry(link, title);
			entry.setThumb(img);
			entry.setIntro(intro);

			entries.add(entry);
		}

		if (entries.size() >= 9) {
			entries.add(new RssEntry(getCode() + URL_SEPARATOR + cat + PAGE_SEPARATOR + (page + 1), NEXT_PAGE_TITLE));
		}

		if (page > 1) {
			entries.add(0, new RssEntry(getCode() + URL_SEPARATOR + cat + PAGE_SEPARATOR + (page - 1), PREV_PAGE_TITLE));
		}

		return entries;
	}

	@Override
	public String getRssLink(String httpLink) {
		return getRssLink(httpLink, "m");
	}

	// http://attitude.co.uk/ben-whishaw-im-worried-gay-typecasting/ => rss://att/m/ben-whishaw-im-worried-gay-typecasting
	protected String getRssLink(String httpLink, String catId) {
		if (StringUtils.isBlank(httpLink)) {
			return null;
		}

		int idx = httpLink.lastIndexOf("-");
		if (idx < 0) {
			return null;
		}
		idx += 1;
		
		int endIdx = httpLink.indexOf(".htm");
		if (endIdx < 0) {
			return null;
		}
		
		String code = httpLink.substring(idx, endIdx);
		if (StringUtils.isBlank(code)) {
			return null;
		}

		return getCode() + URL_SEPARATOR + catId + URL_SEPARATOR + code;
	}

	@Override
	public RssEntry loadEntry(String path, String user) {
		String url = "http://m.kenh14.vn/a/a-" + path + ".htm";

		String html = BaseParser.loadHtml(url, BaseParser.ANDROID_HEADERS, true);

		html = BaseParser.parseContent(html, "<!--chi tiet bai viet-->", "</div> </div>");

		String title = BaseParser.parseContent(html, "<h1 class=\"post-title\">", "</h1>");
		if (StringUtils.isBlank(title)) {
			return null;
		}
		title = title.trim();

		html = BaseParser.parseContent(html, "<h2 class=\"post-sapo\">", null);

		RssEntry entry = new RssEntry();
		entry.setTitle(title);
		entry.setContent(html);

		return entry;
	}

	public static void main(String[] args) throws Exception {
		Kenh14Site site = new Kenh14Site();
		site.afterPropertiesSet();

		RssEntry entry = site.loadEntry("2014072610344554", null);
		System.out.println(entry.getTitle());
		System.out.println(entry.getContent());

//		List<RssEntry> entries = site.listCatEntrys("1", null);
//		if (entries != null) {
//			System.out.println(entries.size());
//			for (RssEntry e : entries) {
//				System.out.println(e.getUrl() + " " + e.getTitle());
//			}
//		}
	}
}
